package com.yjz.media.study.ui;

import android.annotation.SuppressLint;
import android.view.View;

import com.blankj.utilcode.constant.PermissionConstants;
import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PermissionUtils;
import com.yjz.media.study.MediaApplication;
import com.yjz.media.study.R;
import com.yjz.media.study.base.BaseActivity;
import com.yjz.media.study.cons.RTMPConfigure;
import com.yjz.media.study.databinding.ActivityRtmpBinding;
import com.yjz.media.study.jni.X264NativeHelper;
import com.yjz.media.study.rtmp.RTMPNativeHelper;
import com.yjz.media.study.rtmp.RTMPPacket;
import com.yjz.media.study.rtmp.RtmpLivePush;
import com.yjz.media.study.util.camera.CameraHelper;
import com.yjz.media.study.util.camera.CameraImageByteAnalyzer;

@SuppressLint("NonConstantResourceId")
public class RTMPActivity extends BaseActivity<ActivityRtmpBinding> implements View.OnClickListener, CameraImageByteAnalyzer.IPushDataListener {

    //    public static final String RTMP_URL = "rtmp://192.168.0.102:1935/live/kkx";

    private CameraHelper cameraHelper;

    @Override
    public int getLayoutId() {
        return R.layout.activity_rtmp;
    }

    @Override
    public void initView() {

        mBinding.imageCaptureButton.setOnClickListener(this);
        mBinding.startPushButton.setOnClickListener(this);
        mBinding.stopPushButton.setOnClickListener(this);

        // 每次运行删除之前存在的h264文件，始终保持一个最新的。
        FileUtils.delete(MediaApplication.getContext().getExternalCacheDir() + "/camerax_h264.h264");
        FileUtils.delete(MediaApplication.getContext().getExternalCacheDir() + "/camerax_h264.txt");

        FileUtils.delete(MediaApplication.getContext().getExternalCacheDir() + "/x264_enc.h264");
        FileUtils.delete(MediaApplication.getContext().getExternalCacheDir() + "/x264_enc.txt");

        PermissionUtils.permissionGroup(PermissionConstants.CAMERA).callback(new PermissionUtils.SimpleCallback() {
            @Override
            public void onGranted() {
                cameraHelper = CameraHelper.getInstance();
                cameraHelper.setPushDataListener(RTMPActivity.this);
                cameraHelper.startCamera(RTMPActivity.this, RTMPActivity.this, mBinding.viewFinder.getSurfaceProvider());
            }

            @Override
            public void onDenied() {
                showMessageDialog("警告", "请授予权限在使用！", (dialog, which) -> finish());
            }
        }).request();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.image_capture_button: // 拍照
                CameraHelper.getInstance().takePhoto(this);
                break;
            case R.id.start_push_button: // 开始推流
                RtmpLivePush.getInstance().startPush(RTMPConfigure.RTMP_URL);
                break;
            case R.id.stop_push_button: // 结束推流
                RtmpLivePush.getInstance().stopPush();
                cameraHelper.stopPush();
                break;
        }
    }

    @Override
    public void videoDataPush(RTMPPacket rtmpPacket, boolean useDsp) {
        if (useDsp) { // 是否使用DSP进行了编码
            // dsp已经进行了h264编码，直接发送
            RtmpLivePush.getInstance().addPacket(rtmpPacket);
        } else {
            // 需要使用x264进行软编码
            X264NativeHelper.getInstance().encodeData(rtmpPacket.buffer, rtmpPacket.buffer.length, rtmpPacket.type);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RTMPNativeHelper.getInstance().disconnect();
    }
}